package com.szxy.miaosha.redis;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Auther:wzer
 * @Date:2020/9/6
 * @Description:com.szxy.miaosha.config
 * @Version:1.0 Redis 服务
 **/
@Service
public class RedisService {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 获取当时 key
     *
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            return strToBean(value, clazz);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置 key
     *
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean set(String key, T value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String result = beanToStr(value);
            if (result == null || result.length() < 0) {
                return false;
            }
            jedis.set(key, result);
            return true;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 获取当时 key
     *
     * @param prefix
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T get(KeyPrefix prefix, String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = prefix.getPrefix() + ":" + key;
            String value = jedis.get(realKey);
            return strToBean(value, clazz);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置 key
     *
     * @param prefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean set(KeyPrefix prefix, String key, T value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String result = beanToStr(value);
            if (result == null || result.length() < 0) {
                return false;
            }
            String realKey = prefix.getPrefix() + ":" + key;
            jedis.set(realKey, result);
            return true;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 设置 key 并设置过期时间
     *
     * @param prefix
     * @param key
     * @param value
     * @param <T>
     * @return
     */
    public <T> boolean setEx(KeyPrefix prefix, String key, T value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String result = beanToStr(value);
            if (result == null || result.length() < 0) {
                return false;
            }
            String realKey = prefix.getPrefix() + ":" + key;
            jedis.setex(realKey, prefix.expireSeconds(), result);
            return true;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 判断 key 是否存在
     *
     * @param prefix
     * @param key
     * @return
     */
    public boolean existsKey(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = prefix.getPrefix() + ":" + key;
            return jedis.exists(realKey);
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * value 值加 1
     *
     * @param prefix
     * @param key
     * @return
     */
    public Long incr(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = prefix.getPrefix() + ":" + key;
            Long result = jedis.incr(realKey);
            return result;
        } finally {
            returnToPool(jedis);
        }
    }

    /**
     * 原子操作
     * value 值减 1
     *
     * @param prefix
     * @param key
     * @return
     */
    public Long decr(KeyPrefix prefix, String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String realKey = prefix.getPrefix() + ":" + key;
            Long result = jedis.decr(realKey);
            return result;
        } finally {
            returnToPool(jedis);
        }
    }


    private <T> String beanToStr(T value) {
        // 数据校验
        if (value == null) {
            return null;
        }
        if (int.class == value || Integer.class == value) {
            return "" + value;
        } else if (long.class == value || Long.class == value) {
            return "" + value;
        } else if (String.class == value) {
            return (String) value;
        } else {
            return JSON.toJSONString(value);
        }
    }

    private <T> T strToBean(String value, Class<T> clazz) {
        // 数据校验
        if (null == value || null == clazz) {
            return null;
        }
        if (int.class == clazz || Integer.class == clazz) {
            return (T) Integer.valueOf(value);
        } else if (long.class == clazz || Long.class == clazz) {
            return (T) Long.valueOf(value);
        } else if (String.class == clazz) {
            return (T) value;
        } else {
            return JSON.parseObject(value, clazz);
        }
    }


    private void returnToPool(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }


}
